package com.music.activity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.anmusic.application.R;
import com.music.activity.adapter.MainGridAdapter;
import com.music.custom.DragGridView;
import com.music.custom.DragGridView.OnChanageListener;
import com.music.model.FilGridItem;

public class MainActivity extends Activity {
	private List<FilGridItem> dataSourceList = new ArrayList<FilGridItem>();
	private MainGridAdapter mSimpleAdapter;
	private SharedPreferences mSharedPreferences;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_main);

		DragGridView mDragGridView = (DragGridView) findViewById(R.id.main_grid);

		mSimpleAdapter = new MainGridAdapter(this, dataSourceList);
		mDragGridView.setAdapter(mSimpleAdapter);

		mDragGridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View v, int arg2,
					long arg3) {
				FilGridItem filGridItem = (FilGridItem) v
						.getTag(R.id.item_image);
				int values = filGridItem.values;
				switch (values) {
				case 1:
					Intent intent1 = new Intent(MainActivity.this,
							MusicListFragment.class);
					startActivity(intent1);
					break;
				case 2:
					Intent intent2 = new Intent(MainActivity.this,
							ScanMusicActivity.class);
					startActivity(intent2);
					break;
				case 3:

					break;
				case 4:

					break;
				case 5:

					break;

				case 6:

					break;
				default:
					break;
				}

			}
		});
		mDragGridView.setOnChangeListener(new OnChanageListener() {

			@Override
			public void onChange(int from, int to) {
				FilGridItem temp = dataSourceList.get(from);
				// 直接交互item
				// dataSourceList.set(from, dataSourceList.get(to));
				// dataSourceList.set(to, temp);

				// 这里的处理需要注意下
				if (from < to) {
					for (int i = from; i < to; i++) {
						Collections.swap(dataSourceList, i, i + 1);
					}
				} else if (from > to) {
					for (int i = from; i > to; i--) {
						Collections.swap(dataSourceList, i, i - 1);
					}
				}

				dataSourceList.set(to, temp);

				mSimpleAdapter.notifyDataSetChanged();

			}
		});
	}

	@Override
	protected void onResume() {
		super.onResume();
		getData();
		mSimpleAdapter.notifyDataSetChanged();

	}

	private void getData() {
		dataSourceList.clear();
		mSharedPreferences = getSharedPreferences("MyGridPoint",
				Context.MODE_PRIVATE);
		int mainVal1 = mSharedPreferences.getInt("0x1", 0);
		AddData(mainVal1, "0x1");
		int mainVal2 = mSharedPreferences.getInt("0x2", 0);
		AddData(mainVal2, "0x2");
		int mainVal3 = mSharedPreferences.getInt("0x3", 0);
		AddData(mainVal3, "0x3");
		int mainVal4 = mSharedPreferences.getInt("0x4", 0);
		AddData(mainVal4, "0x4");
		int mainVal5 = mSharedPreferences.getInt("0x5", 0);
		AddData(mainVal5, "0x5");
		int mainVal6 = mSharedPreferences.getInt("0x6", 0);
		AddData(mainVal6, "0x6");
	}

	private void AddData(int postion, String key) {
		switch (postion) {
		case 1:
			dataSourceList.add(new FilGridItem(key, 1, R.drawable.main_draw1,
					"我的音乐"));
			break;
		case 2:
			dataSourceList.add(new FilGridItem(key, 2, R.drawable.main_draw2,
					"推荐音乐"));
			break;
		case 3:
			dataSourceList.add(new FilGridItem(key, 3, R.drawable.main_draw3,
					"他人音乐"));
			break;
		case 4:
			dataSourceList.add(new FilGridItem(key, 4, R.drawable.main_draw4,
					"收藏音乐"));
			break;
		case 5:
			dataSourceList.add(new FilGridItem(key, 5, R.drawable.main_draw5,
					"音乐排行"));
			break;
		case 6:
			dataSourceList.add(new FilGridItem(key, 6, R.drawable.main_draw6,
					"贡献音乐"));
			break;
		}
	}

}
